using System.Net.Sockets;
using System.Net;
using System.Text.Json;
using System.Text;

namespace Chrome
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                var uri = new Uri("http://127.0.0.1:8080");
                var client = new Socket(
                    AddressFamily.InterNetwork, 
                    SocketType.Stream, 
                    ProtocolType.Tcp);
                var ipaddress = IPAddress.Parse(uri.Host);
                client.Connect(new IPEndPoint(ipaddress, uri.Port));
                var request = new HttpRequest(textBox1.Text, "Get", textBox2.Text);
                var package = JsonSerializer.Serialize(request);
                client.Send(Encoding.UTF8.GetBytes(package));
                var buffer = new byte[1027 * 1024];
                int count = client.Receive(buffer);
                if (count>0)
                {
                    textBox3.Text = Encoding.UTF8.GetString(buffer, 0, count);
                }
            }
            catch
            {
            }
        }
    }
}